package com.youzikj.wx.core.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;

import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bos.BosClient;
import com.baidubce.services.bos.BosClientConfiguration;
import com.baidubce.services.bos.model.PutObjectResponse;

public class BDyunUtil {
	
	private static String ACCESS_KEY_ID = "320a7a13b81f4cceae4bdf00cd500b3c";
	private static String SECRET_ACCESS_KEY = "1bb56f3f0a524fecb2abf5f1fe3bb2eb";
	private static String ENDPOINT = "http://gz.bcebos.com";
	
	public static void upload(String name,String path){
		// 初始化一个BosClient
				BosClientConfiguration config = new BosClientConfiguration();
				config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,
						SECRET_ACCESS_KEY));
				config.setEndpoint(ENDPOINT);
				BosClient client = new BosClient(config);
				 try {
				 putObject(client, "wycd-yzkj", name, path);
				 } catch (FileNotFoundException e) {
				 }
	  }
	
	//上传文件
		public static void putObject(BosClient client, String bucketName,
				String objectKey,String path) throws FileNotFoundException {
			// 获取指定文件
			File file = new File(path);
			// 以文件形式上传Object
			PutObjectResponse putObjectFromFileResponse = client.putObject(
					bucketName, objectKey, file);
			// 打印ETag
			System.out.println(putObjectFromFileResponse.getETag());
		}

		/**
		 * 获取指定文件的url
		 * @param client
		 * @param bucketName
		 * @param objectKey
		 * @param expirationInSeconds
		 * @return
		 */
		public static String generatePresignedUrl(//BosClient client, String bucketName,, int expirationInSeconds
				String objectKey) {
			BosClientConfiguration config = new BosClientConfiguration();
			config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,
					SECRET_ACCESS_KEY));
			config.setEndpoint(ENDPOINT);
			BosClient client = new BosClient(config);
			URL url = client.generatePresignedUrl("wycd-yzkj", objectKey, -1);
			//指定用户需要获取的Object所在的Bucket名称、该Object名称、时间戳、URL的有效时长  
			return url.toString();
		}

}
